package com.zyj.myapplication.Activity;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.zyj.myapplication.R;
import com.zyj.myapplication.Javabean.Train;
import com.zyj.myapplication.Javabean.TrainInfo;
import com.zyj.myapplication.Model.TrainModel;

import it.sephiroth.android.library.picasso.Picasso;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class TrainListActivity extends AppCompatActivity {
    private ImageView trainPhoto, btn_back;
    private TextView trainIntro, trainNotice, trainplace, traintime, price, trainname;
    private LinearLayout train_zixun, train_baoming;
    private String train_id;
    private TrainInfo traininfo;
    private Context mContext;

    //修改123
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_train_list);
        initViews();
        initEvents();
        initData();
    }

    private void initViews() {
        trainPhoto = (ImageView) findViewById(R.id.trainPhoto);
        trainIntro = (TextView) findViewById(R.id.trainIntro);
        trainNotice = (TextView) findViewById(R.id.trainNotice);
        trainplace = (TextView) findViewById(R.id.train_place);
        traintime = (TextView) findViewById(R.id.train_time);
        train_zixun = (LinearLayout) findViewById(R.id.train_zixun);
        train_baoming = (LinearLayout) findViewById(R.id.train_baoming);
        price = (TextView) findViewById(R.id.price);
        trainname = (TextView) findViewById(R.id.train_name);
        btn_back = (ImageView) findViewById(R.id.train_back);
    }

    private void initEvents() {
        train_zixun.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(TrainListActivity.this, "电话", Toast.LENGTH_SHORT).show();
                Intent phone = new Intent(Intent.ACTION_DIAL);
                Uri data = Uri.parse("tel:" + "15146318678");
                phone.setData(data);
                startActivity(phone);
            }
        });

        train_baoming.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(TrainListActivity.this, PlaceOrderActivity.class);
                intent.putExtra("train_id", train_id);
                intent.putExtra("price", traininfo.getPrice());

                startActivity(intent);
            }
        });
        btn_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    private void initData() {
        Intent intent = getIntent();
        train_id = intent.getStringExtra("train_id");

        showData();
    }

    private void showData() {
        TrainModel trainModel = new TrainModel();
        Call<TrainInfo> call = trainModel.gettrainlist(train_id);
        call.enqueue(new Callback<TrainInfo>() {
            @Override
            public void onResponse(Call<TrainInfo> call, Response<TrainInfo> response) {
                traininfo = response.body();
                trainname.setText(traininfo.getName());
                trainIntro.setText(traininfo.getIntro());
                trainNotice.setText(traininfo.getNotice());
                traintime.setText(traininfo.getTraintime());
                trainplace.setText(traininfo.getPlace());
                price.setText("￥" + traininfo.getPrice());
                String address = "http://172.24.10.175/workout/Uploads/" + traininfo.getThumb();
                if (!traininfo.getThumb().equals("")) {
                    Picasso.with(mContext).load(address).into(trainPhoto);
                }
            }

            @Override
            public void onFailure(Call<TrainInfo> call, Throwable t) {

            }
        });
    }
}

